//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<memory>
#include<string>
#include<vector>
#include<cctype>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

typedef unsigned long long      ui64;
typedef long long               i64;
typedef	vector<int>             VI;
typedef	vector<bool>            VB;
typedef	vector<VI>              VVI;
typedef	vector<string>          VS;
typedef	pair<int,int>           PII;
typedef map<string,int>         MSI;
typedef set<int>                SI;
typedef set<string>             SS;
typedef map<int,int>            MII;
typedef	pair<double,double>     PDD;

#define PB                      push_back
#define MP                      make_pair
#define X                       first
#define Y                       second
#define FOR(i, a, b)            for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b)           for(int i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b)             memset(a, b, sizeof(a))
#define SZ(a)                   int((a).size())
#define ALL(a)                  (a).begin(), (a).end()
#define RALL(a)                 (a).rbegin(), (a).rend()
#define INF                     (2000000000)

int main()
{
	int n;
	cin >> n;
	VI a(n);
	FOR(i,0,n) {
		scanf("%d",&a[i]);
		a[i]--;
		if(!i)continue;
	}
	/*
	if(a[0]==a.back())
	{
		cout << a[0]+1 <<endl;
		return 0;
	}
	*/
	//
	VI dp(n,0);
	VI best(10000,INF);
	VI place(10000,INF);
	VI from(n,0);
	dp[0] = 1;
	best[a[0]] = 0;
	place[a[0]] = 0;
	FOR(i,1,n) {
		if( dp[i-1]+1 >= best[a[i]] ) {
			dp[i] = best[a[i]];
			from[i] = place[a[i]];
			continue;
		}
		dp[i] = dp[i-1]+1;
		from[i] = i-1;
		best[a[i]] = dp[i];
		place[a[i]] = i;
	}
	
	int it = from.back();
	VI res;
	res.PB(a.back());
	while(it!=0 ) {
		if(a[it]!=res.back())
			res.PB( a[it] );
		it = from[it];
	}
	if(res.back()!=a[0])
		res.PB( a[0] );
	reverse(ALL(res));
	FOR(i,0,SZ(res))
		printf("%d%c",res[i]+1,i==SZ(res)-1?'\n':' ');
	
    return 0;
}